package com.ruoyi.dj.service.impl;

import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.dj.domain.ExcelTable;
import com.ruoyi.dj.domain.TCddw;
import com.ruoyi.dj.mapper.ExcelTableMapper;
import com.ruoyi.dj.mapper.TCddwMapper;
import com.ruoyi.dj.service.IExcelTableService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 新建格Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-29
 */
@Service
public class ExcelTableServiceImpl implements IExcelTableService {
    @Autowired
    private ExcelTableMapper excelTableMapper;
    @Autowired
    private TCddwMapper tCddwMapper;
    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 查询新建格
     *
     * @param tableId 新建格主键
     * @return 新建格
     */
    @Override
    public ExcelTable selectExcelTableByTableId(ExcelTable excelTable) {
        return excelTableMapper.selectExcelTableByTableId(excelTable);
    }

    /**
     * 查询新建格列表
     *
     * @param excelTable 新建格
     * @return 新建格
     */
    @Override
    public List<ExcelTable> selectExcelTableList(ExcelTable excelTable) {
        return excelTableMapper.selectExcelTableList(excelTable);
    }

    @Override
    public ExcelTable selectExcelTableOne(Long id) {
        return excelTableMapper.selectExcelTableOne(id);
    }

    @Override
    public ExcelTable selectExcelTable(ExcelTable excelTable) {
        return excelTableMapper.selectExcelTable(excelTable);
    }

    /**
     * 新增新建格
     *
     * @param excelTable 新建格
     * @return 结果
     */
    @Override
    public int insertExcelTable(ExcelTable excelTable, SysDictType sysDictType) {
        //创建表格
        excelTable.setCreateTime(DateUtils.getNowDate());
        int i = excelTableMapper.insertExcelTable(excelTable);
        batchCddw(excelTable.getTableId(), excelTable.getCddw());
        dictTypeService.insertDictType(sysDictType);
        return i;
    }

    @Override
    public void batchCddw(Long tableId, List<Long> cddwIds) {
        if (cddwIds.size() > 0) {
            // 新增用户与角色管理
            List<TCddw> list = new ArrayList<TCddw>(cddwIds.size());
            for (Long cddw : cddwIds) {
                TCddw ur = new TCddw();
                ur.setTableId(tableId);
                ur.setDeptId(cddw);
                list.add(ur);
            }
            tCddwMapper.batchCddw(list);
        }
    }

    /**
     * 修改新建格
     *
     * @param excelTable 新建格
     * @return 结果
     */
    @Override
    public int updateExcelTable(ExcelTable excelTable, SysDictType sysDictType) {
        excelTable.setUpdateTime(DateUtils.getNowDate());
        ExcelTable excelTable1 = excelTableMapper.ExcelTableOne(excelTable.getTableId());
        System.out.println("excelTable1.getTableName() = " + excelTable1.getTableName());
        System.out.println("excelTable.getTableName() = " + excelTable.getTableName());
        if (!excelTable1.getTableName().equals(excelTable.getTableName())) {
            dictTypeService.updateDictType(sysDictType);
        }
        int i = excelTableMapper.updateExcelTable(excelTable);
        updateTD(excelTable.getTableId(), excelTable.getCddw());
        return i;
    }

    public void updateTD(Long tableId, List<Long> cddwIds) {
        if (cddwIds.size() > 0) {
            List<TCddw> tCddws = tCddwMapper.seleteTD(tableId);
            //需要新增的
            List<TCddw> list = new ArrayList<TCddw>(cddwIds.size());
            boolean exists = false;
            for (Long cddw : cddwIds) {
                for (TCddw tCddw : tCddws) {
                    if (tCddw.getDeptId().equals(cddw)) {
                        exists = true;
                        break;
                    }
                }
                if (exists == false) {
                    TCddw ur = new TCddw();
                    ur.setTableId(tableId);
                    ur.setDeptId(cddw);
                    list.add(ur);
                }
                exists = false;
            }
            System.out.println("list.size() = " + list.size());
            boolean b1 = list.size() > 0;
            System.out.println("list.size() > 0 = " + b1);
            if (list.size() > 0) {
                for (TCddw t : list){
                    System.out.println("t = " + t);
                }
                tCddwMapper.batchCddw(list);
            }
            //需要删除的
            Long[] bgCcdwIds = tCddws.stream().filter(tCddw -> !cddwIds.contains(tCddw.getDeptId())).map(TCddw::getBgCcdwId).toArray(Long[]::new);
            System.out.println("bgCcdwIds = " + bgCcdwIds.length);
            boolean b2 = bgCcdwIds.length > 0;
            System.out.println("bgCcdwIds = " + b2);
            if (bgCcdwIds.length > 0) {
                for (Long b : bgCcdwIds){
                    System.out.println("b = " + b);
                }
                tCddwMapper.deleteTCddwByBgCcdwIds(bgCcdwIds);
            }
        }
    }

    /**
     * 批量删除新建格
     *
     * @param tableIds 需要删除的新建格主键
     * @return 结果
     */
    @Override
    public int deleteExcelTableByTableIds(Long[] tableIds) {
        return excelTableMapper.deleteExcelTableByTableIds(tableIds);
    }

    /**
     * 删除新建格信息
     *
     * @param tableId 新建格主键
     * @return 结果
     */
    @Override
    public int deleteExcelTableByTableId(Long tableId) {
        return excelTableMapper.deleteExcelTableByTableId(tableId);
    }


}